import os
import json
import yaml


def cond_mkdir(path, is_file: bool = False):
    path = os.path.normpath(path)
    if is_file:
        path = os.path.dirname(path)
        if path == "":
            path = "./"
    if not os.path.exists(path):
        os.makedirs(path)
    return path


def load_json(filename):
    with open(filename, "r") as f:
        res = json.load(f)
    return res


def save_json(filename, data):
    with open(filename, "w") as f:
        json.dump(data, f)


def load_yaml(filename):
    with open(filename, "r") as stream:
        res = yaml.safe_load(stream)
    return res


def save_yaml(filename, data):
    with open(filename, "w") as outfile:
        yaml.dump(data, outfile, default_flow_style=False)
